package com.wangshuqi.jdbc.dao;

import com.wangshuqi.jdbc.bean.Customer;
import com.wangshuqi.jdbc.transaction.JDBCTransactionUtil;
import com.wangshuqi.jdbc.util.JDBCUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

public class CustomerDAOTest {

    private CustomerDAO customerDAO = new CustomerDAOImpl();

    @Test
    public void insertCustomer() { // 事务处理
        Connection connection = null;
        try {
            connection = JDBCTransactionUtil.getConnection();
            connection.setAutoCommit(false);
            Customer customer = new Customer();
            customer.setName("张三");
            customer.setEmail("zhangSan@123.com");
            customer.setBirth(new Date(System.currentTimeMillis()));
            int insertCustomer = customerDAO.insertCustomer(connection, customer);
            if (insertCustomer > 0) {
                System.out.println("新增成功");
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            JDBCTransactionUtil.closeConnection(connection);
        }
    }

    @Test
    public void updateById() {
        Connection connection = null;
        try {
            connection = JDBCTransactionUtil.getConnection();
            connection.setAutoCommit(false);
            Integer id = 12;
            Customer customer = customerDAO.selectById(connection, id);
            System.out.println("原数据：" + customer);
            customer.setName("谢霆锋");
            int updateById = customerDAO.updateById(connection, customer);
            if (updateById > 0) {
                System.out.println("修改成功");
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            JDBCTransactionUtil.closeConnection(connection);
        }
    }

    @Test
    public void deleteById() {
        Connection connection = null;
        try {
            connection = JDBCTransactionUtil.getConnection();
            connection.setAutoCommit(false);
            Integer id = 13;
            int deleteById = customerDAO.deleteById(connection, id);
            if (deleteById > 0) {
                System.out.println("删除成功");
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            JDBCTransactionUtil.closeConnection(connection);
        }
    }


    @Test
    public void selectAll() {
        Connection connection = null;
        try {
            connection = JDBCTransactionUtil.getConnection();

            List<Customer> customers = customerDAO.selectAll(connection);
            customers.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTransactionUtil.closeConnection(connection);
        }


    }

    @Test
    public void count() {
        Connection connection = null;
        try {
            connection = JDBCTransactionUtil.getConnection();

            Long count = customerDAO.count(connection);
            System.out.println("共有：" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTransactionUtil.closeConnection(connection);
        }
    }

}
